using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using sbMembership.Library.Presenters.ViewInterfaces;
using sbMembership.Library.Presenters;
using sbMembership.Library.Domain;

namespace sbMembership.Web.Views
{
    public partial class AssignUserToRoleView : System.Web.UI.UserControl, IAssignRolesToUserView
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        #region IAssignUserToRoleView Members

        public string Message
        {
            set { lblMessage.Text = value; }
        }

        public string UserToAssign
        {
            get
            {
                return fldUserName.Value;
            }
            set
            {
                fldUserName.Value = value;
            }
        }

        public string[] AvailableRoles
        {
            set
            {
                lstRoles.DataSource = value;
                lstRoles.DataBind();
            }
        }

        public string[] AssignedRoles
        {
            set
            {
                List<sbRole> roleList = new List<sbRole>();
                foreach (string role in value)
                {
                    roleList.Add(new sbRole(role));
                }
                gvRoles.DataSource = roleList;
                gvRoles.DataBind();
            }
        }

        AssignRolesToUserPresenter presenter;
        public void AttachPresenter(AssignRolesToUserPresenter presenter)
        {
            this.presenter = presenter;
        }

        #endregion

        protected void btnAssignRoles_Click(object sender, EventArgs e)
        {
            presenter.AssignRole(lstRoles.SelectedValue);
        }

        protected void gvRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void gvRoles_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int rowID = Int32.Parse(e.CommandArgument.ToString());
                GridViewRow gr = gvRoles.Rows[rowID];
                string roleName = gr.Cells[0].Text;
                presenter.RemoveRole(roleName);
            }

        }
    }
}